/**
 * 
 */
package com.swim.shop.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.swim.shop.entity.ProductOrder;
import com.swim.shop.service.ProductOrderService;

/**
 * 产品订单Controller
 * @author ljw
 * @version 2023-05-24
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/productOrder")
public class ProductOrderController extends BaseController {

	@Autowired
	private ProductOrderService productOrderService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public ProductOrder get(String id, boolean isNewRecord) {
		return productOrderService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("shop:productOrder:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProductOrder productOrder, Model model) {
		model.addAttribute("productOrder", productOrder);
		return "swim/shop/productOrderList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("shop:productOrder:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<ProductOrder> listData(ProductOrder productOrder, HttpServletRequest request, HttpServletResponse response) {
		Page<ProductOrder> page = productOrderService.findPage(new Page<ProductOrder>(request, response), productOrder); 
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("shop:productOrder:view")
	@RequestMapping(value = "form")
	public String form(ProductOrder productOrder, Model model) {
		model.addAttribute("productOrder", productOrder);
		return "swim/shop/productOrderForm";
	}

	/**
	 * 保存产品订单
	 */
	@RequiresPermissions("shop:productOrder:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated ProductOrder productOrder) {
		return renderResult(Global.TRUE, text("保存产品订单成功！"), productOrderService.saveAuto(productOrder));
	}
	
	/**
	 * 获取详情数据
	 * @param orderNum 订单号
	 */
	@RequestMapping(value = "detailsData")
	@ResponseBody
	public String detailsData(String orderNum) {
		ProductOrder productOrder = new ProductOrder();
        productOrder.setOrderNum(orderNum);
        List<ProductOrder> productOrderList = productOrderService.findList(productOrder);
		return renderResult(Global.TRUE, text("获取详情数据成功！"), productOrderService.get(productOrderList.get(0).getId()));
	}
 	
	/**
	 * 查询订单支付状态
	 * @param outTradeNo
	 * @return
	 */
	@GetMapping("/query/product/order/pay/status")
	public String orderStatusQuery(@RequestParam String orderNum) {
		ProductOrder productOrder = new ProductOrder();
        productOrder.setOrderNum(orderNum);
        List<ProductOrder> productOrderList = productOrderService.findList(productOrder);
        if (CollectionUtils.isNotEmpty(productOrderList) && productOrderList.get(0).getOrderStatus() == 2L) {// 订单状态 2表示已付款
        	return "SUCCESS";
		}
        return "TRADE_NOT_EXIST";
	}
	
}